﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;


using System.Xml.Linq;
using CheckOilPrices.PTTService;
using System.Threading;
using System.Windows.Threading;

namespace CheckOilPrices
{
    public class FunctionClass
    {

        public int cnt_client = 0;
        public int m2;
        string datenow = DateTime.Now.ToString("dd/MM/yyyy");
        string diclan;
        Dispatcher disp;
        public void feedOilPrice(string a,DatePicker datePicker)
        {
            try
            {
//                disp.BeginInvoke(() =>
//                {
                    //main_list_oils = null;
                    diclan = a;
                    datePicker.ValueStringFormat = "{0:dd/MM/yyyy}";
                    string date = datePicker.ValueString;
                    short day = Convert.ToInt16(date.Substring(0, 2));
                    short month = Convert.ToInt16(date.Substring(3, 2));
                    short year = Convert.ToInt16(date.Substring(6, 4));
                    
                    PTTService.PTTInfoSoapClient proxy = new CheckOilPrices.PTTService.PTTInfoSoapClient();
                    proxy.GetOilPriceAsync(diclan, day, month, year);
                    proxy.GetOilPriceCompleted += new EventHandler<PTTService.GetOilPriceCompletedEventArgs>(proxy_PTTServiceCompleted);
                    cnt_client++;
//                  });
            }
            catch { 
            
            }
        }
        public List<ResourceClass> main_list_oils = new List<ResourceClass>();
        public List<ResourceClass> main_list_gas = new List<ResourceClass>();
        void proxy_PTTServiceCompleted(object sender, CheckOilPrices.PTTService.GetOilPriceCompletedEventArgs e)
        {
            try
            {
                string a = e.Result;
                XElement elm = XElement.Parse(e.Result);
                var oilPrice = from oilpricedb in elm.Descendants("Table")
                               select new ResourceClass
                               {
                                   O_Product = oilpricedb.Element("PRODUCT").Value,
                                   O_Price = oilpricedb.Element("PRICE").Value
                               };
                main_list_oils.Clear();
                main_list_gas.Clear();
                foreach (ResourceClass rs in oilPrice)
                {
                    if (rs.O_Product.Equals("Blue Gasoline 91") || rs.O_Product.Equals("Blue Diesel") || rs.O_Product.Equals("Blue Gasohol 91") || rs.O_Product.Equals("Blue Gasohol E20") || rs.O_Product.Equals("Blue Gasohol 95") || rs.O_Product.Equals("Blue Gasohol E85"))
                    {
                        main_list_oils.Add(rs);
                    }
                    else if (rs.O_Product.Equals("NGV"))
                    {
                        main_list_gas.Add(rs);
                    }
                }
                cnt_client--;
            }
            catch (WebException)
            {
                MessageBox.Show("Please check your internet connection.");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                return;
            }
        }

        public void feedBJPrice() {
            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri("http://www.bangchak.co.th/oilprice-widget.aspx"));
            client.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }
        public List<ResourceClass> main_list_BJoils = new List<ResourceClass>();
        public List<ResourceClass> main_list_BJgas = new List<ResourceClass>();
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
            try
            {
                string htmlBJ = e.Result;
                searchText(htmlBJ);
                if (BJOilPrice[8].Equals(null))
                {
                    for (int i = 0; i < 8; i++)
                    {
                        if (i == 0) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "SuperPower B5"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 1) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "SuperPower D"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 2) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol E85"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 3) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol E20"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 4) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol 91"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 5) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol 95"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 6) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "GreenPlus 91"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 7) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "NGV"; BJOil.O_Price = BJOilPrice[i]; main_list_BJgas.Add(BJOil); }

                    }
                }
                else 
                {
                    for (int i = 0; i < 7; i++)
                    {
                        if (i == 0) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "SuperPower D"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 1) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol E85"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 2) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol E20"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 3) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol 91"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 4) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "Gasohol 95"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 5) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "GreenPlus 91"; BJOil.O_Price = BJOilPrice[i]; main_list_BJoils.Add(BJOil); }
                        else if (i == 6) { ResourceClass BJOil = new ResourceClass(); BJOil.O_Product = "NGV"; BJOil.O_Price = BJOilPrice[i]; main_list_BJgas.Add(BJOil); }

                    }
                }
            }
            catch (WebException)
            {
                MessageBox.Show("Please check your internet connection.");
                throw new ExitExeption();
                //return;
            }
            catch (Exception)
            {
                MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                return;
            }
        }

        public class ExitExeption : Exception { }

        int position;
        int position2;
        string org;

        String[] BJOilPrice = new String[9];
        void searchText(string a)
        {
            for (int i = 0;i<9 ; i++)
            {
                if (a.IndexOf("css2"+"\""+">") != -1)
                {
                    org = a;
                    position = a.IndexOf("css2" + "\"" +">");
                    a = a.Substring(position, a.Length-position);
                    position2 = a.IndexOf("</td>");
                    a = a.Substring(position2, a.Length-position2);
                }
                BJOilPrice[i] = org.Substring(position + 6, position2 - 6);
            }
        }
    }
}
